package com.sc.nft.task;

import com.sc.nft.dao.RankingBoardConfDao;
import com.sc.nft.dao.RankingBoardDetailsNumberDao;
import com.sc.nft.dao.RankingBoardHistoryDetailsDao;
import com.sc.nft.entity.RankingBoardConf;
import com.sc.nft.entity.RankingBoardHistoryDetails;
import com.sc.nft.service.RankingBoardDetailsNumberService;
import com.sc.nft.util.WeChatUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.sc.nft.service.impl.RankingBoardDetailsNumberServiceImpl.tableSegmentation;

@Component
@RequiredArgsConstructor
@Slf4j
public class RankingListTask {

    private final RankingBoardConfDao rankingBoardConfDao;

    private final RankingBoardDetailsNumberService rankingBoardDetailsNumberService;

    private final RankingBoardDetailsNumberDao rankingBoardDetailsNumberDao;

    private final RankingBoardHistoryDetailsDao rankingBoardHistoryDetailsDao;

    /**
     * 排行榜更新
     */
    @XxlJob("rankingUpdate")
    public void rankingUpdate() {
        /*
        1.获取所有整在进行的排行榜
        2.迭代每个排行榜,排行榜的排名
        3.更新排行榜,更新排行榜的时候,根据当前时间写入不同的奇数表or偶数表.在把另外一个表的数据清空.

        注:要考虑市值账号过滤
         */

        try {
            rankingBoardDetailsNumberDao.deleteByRankingBoardConfId(tableSegmentation(Boolean.FALSE));
            // 获取正在进行的排行榜
            List<RankingBoardConf> rankingBoardConfs = rankingBoardConfDao.listIsOngoing();

            // 封装排行榜的排名
            rankingBoardConfs.forEach(rankingBoardConf -> {
                rankingBoardDetailsNumberService.rankingUpdate(rankingBoardConf);
            });
        } catch (Exception e) {
            WeChatUtil.sendWeChatMessage("排行榜更新异常:" + e);
            log.error("排行榜更新异常", e);
        }
    }

    /**
     * 结算排行榜
     */
    @XxlJob("settlementList")
    public void settlementList() {
        /*
            1.获取所有以过期且未结算的排行榜
            2.迭代每个排行榜,把数据结算掉.写入结算表
            结算的时候,有定榜操作,以榜单结束时间,为准.
         */
        try {
            // 获取所有以过期且未结算的排行榜
            List<RankingBoardConf> rankingBoardConfs = rankingBoardConfDao.listIsExpiredAndNotSettlement();

            // 封装排行榜的排名
            rankingBoardConfs.forEach(rankingBoardConf -> {
                rankingBoardDetailsNumberService.settlementList(rankingBoardConf);
            });
        } catch (Exception e) {
            WeChatUtil.sendWeChatMessage("排行榜结算异常:" + e);
            log.error("排行榜结算异常", e);
        }
    }

    /**
     * 发放排名奖励
     */
    @XxlJob("rankingAward")
    public void rankingAward() {
        /*
        1.获取所有待发放奖励的排行榜
        2.迭代每个排行榜,根据状态类型.发放奖励
         */
        try {
            List<RankingBoardHistoryDetails> rankingBoardHistoryDetails = rankingBoardHistoryDetailsDao.leaderboardsToBeAwarded();
            rankingBoardHistoryDetails.forEach(rankingBoardHistoryDetail -> {
                rankingBoardDetailsNumberService.rankingAward(rankingBoardHistoryDetail);
            });
        } catch (Exception e) {
            WeChatUtil.sendWeChatMessage("发放奖励异常:" + e);
            log.error("发放奖励", e);
        }
    }

}
